package leetcode.每日一题;

import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2021/11/21 9:55
 * @description：https://leetcode-cn.com/problems/maximum-depth-of-n-ary-tree/
 */
public class N叉树的最大深度 {
    @Test
    public void test() {
        Solution solution = new Solution();
    }


    /*
    //DFS
    class Solution {
        public int maxDepth(Node root) {
            if (root == null) return 0;
            List<Node> childrens = root.children;
            if (childrens == null || childrens.size() == 0) return 1;
            int max = 0;
            for (Node children : childrens) {
                max = Math.max(maxDepth(children) + 1, max);
            }
            return max;
        }
    }
    */

    class Solution {
        public int maxDepth(Node root) {
            if (root == null) return 0;
            Queue<Node> queue = new LinkedList<>();
            queue.offer(root);
            int maxDepth = 1;
            while (!queue.isEmpty()) {
                int size = queue.size();
                for (int i = 0; i < size; i++) {
                    Node cur = queue.poll();
                    List<Node> children = cur.children;
                    for (Node child : children) {
                        queue.offer(child);
                    }
                }
                maxDepth++;
            }
            return maxDepth;
        }
    }

    class Node {
        public int val;
        public List<Node> children;

        public Node() {}

        public Node(int _val) {
            val = _val;
        }

        public Node(int _val, List<Node> _children) {
            val = _val;
            children = _children;
        }
    }

}
